<?php
//UMA new
namespace App\Http\Requests;

use App\Models\InputItem;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ScoreInfoPostRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            //'temporary_member_id' => 'required|exists:customers,temporary_member_id',
            'sex' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'sex')->first()->id);
            })],
            'year_of_birth' => 'required|numeric|max:' . date('Y', time()),
            'nation' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'nation')->first()->id);
            })],
            'marriage' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'marriage')->first()->id);
            })],
            'family_structure' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'family_structure')->first()->id);
            })],
            'family_members_quantity' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'family_members_quantity')->first()->id);
            })],
            'raise' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'raise')->first()->id);
            })],
            'driver_lesson_number' => 'nullable|numeric|digits_between:1,12',
            'working_school_name' => 'required|max:50',
            'working_school_name_kana' => 'required|max:50|kana',
            'working_school_zip' => 'required|numeric|digits_between:1,7',
            'working_school_pref' => 'required|max:50',
            'working_school_city' => 'required|max:50',
            'working_school_town' => 'required|max:50',
            'working_school_tel' => 'required|numeric|digits_between:10,11',
            'hirement' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'hirement')->first()->id);
            })],
            'work_type' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'work_type')->first()->id);
            })],
            'work_responsibility' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'work_responsibility')->first()->id);
            })],
            'work_department' => 'required|max:50',
            'work_place_type' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'work_place_type')->first()->id);
            })],
            'employment_capital' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'employment_capital')->first()->id);
            })],
            'employee_quantity' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'employee_quantity')->first()->id);
            })],
            'employment_start_date' => 'required|date',
            'yearly_income' => 'required|numeric|min:0|max:9999999999',
            'salary_type' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'salary_type')->first()->id);
            })],
            'insurance' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'insurance')->first()->id);
            })],
            'residence_place' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'residence_place')->first()->id);
            })],
            'rent' => 'required|numeric|min:0|max:9999999999',
            'balance' => 'required|numeric|min:0|max:9999999999',
            'usage_term_accepted' => 'required|numeric|min:0|max:1',
            'has_borrowing' => ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'has_borrowing')->first()->id);
            })]
        ];
        if ($this->get('working_school_building')) {
            $rules['working_school_building'] = 'max:50';
        }
        if ($this->get('has_borrowing') == 1) {
            $rules['auto_loan'] = ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'auto_loan')->first()->id);
            })];
            $rules['cash_loan'] = ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'cash_loan')->first()->id);
            })];
            $rules['other_loan'] = ['required', Rule::exists('input_options', 'option')->where(function ($query) {
                $query->where('input_item_id', InputItem::where('identifier', 'other_loan')->first()->id);
            })];
        }
        if (in_array($this->get('auto_loan'), [1, 2])) {
            $rules['monthly_autoloan_repayment'] = 'required|numeric|min:0|max:9999999999';
        }

        if (in_array($this->get('cash_loan'), [1, 2])) {
            $rules['monthly_cashloan_repayment'] = 'required|numeric|min:0|max:9999999999';
        }
        if (in_array($this->get('other_loan'), [1, 2])) {
            $rules['monthly_otherloan_repayment'] = 'required|numeric|min:0|max:9999999999';
        }
        return $rules;
    }

    /**
     * defined the error message
     *
     * @return array
     */
    public function messages()
    {
        $msg = [
            'sex.required' => 'E-INPUT-00001-REQ',
            'sex.exists' => 'E-INPUT-00001-INV',
            'year_of_birth.required' => 'E-INPUT-00002-REQ',
            'year_of_birth.numeric' => 'E-INPUT-00002-NUM',
            'year_of_birth.max' => 'E-INPUT-00002-MAX',
            'year_of_birth.min' => 'E-INPUT-00002-MIN',
            'nation.required' => 'E-INPUT-00003-REQ',
            'nation.exists' => 'E-INPUT-00003-INV',
            'marriage.required' => 'E-INPUT-00004-REQ',
            'marriage.exists' => 'E-INPUT-00004-INV',
            'family_structure.required' => 'E-INPUT-00005-REQ',
            'family_structure.exists' => 'E-INPUT-00005-INV',
            'family_members_quantity.required' => 'E-INPUT-00006-REQ',
            'family_members_quantity.exists' => 'E-INPUT-00006-INV',
            'raise.required' => 'E-INPUT-00007-REQ',
            'raise.exists' => 'E-INPUT-00007-INV',
            'driver_lesson_number.required' => 'E-INPUT-00008-REQ',
            'driver_lesson_number.numeric' => 'E-INPUT-00008-NUM',
            'driver_lesson_number.digits_between' => 'E-INPUT-00008-MAX',
            'working_school_name.required' => 'E-INPUT-00009-REQ',
            'working_school_name.max' => 'E-INPUT-00009-MAX',
            'working_school_name_kana.required' => 'E-INPUT-00010-REQ',
            'working_school_name_kana.max' => 'E-INPUT-00010-MAX',
            'working_school_name_kana.kana' => 'E-INPUT-00010-KAN',
            'working_school_zip.required' => 'E-INPUT-00011-REQ',
            'working_school_zip.numeric' => 'E-INPUT-00011-NUM',
            'working_school_zip.digits_between' => 'E-INPUT-00011-MAX',
            'working_school_pref.required' => 'E-INPUT-00012-REQ',
            'working_school_pref.max' => 'E-INPUT-00012-MAX',
            'working_school_city.required' => 'E-INPUT-00013-REQ',
            'working_school_city.max' => 'E-INPUT-00013-MAX',
            'working_school_town.required' => 'E-INPUT-00014-REQ',
            'working_school_town.max' => 'E-INPUT-00014-MAX',
            'working_school_building.max' => 'E-INPUT-00015-MAX',
            'working_school_tel.required' => 'E-INPUT-00016-REQ',
            'working_school_tel.numeric' => 'E-INPUT-00016-NUM',
            'working_school_tel.digits_between' => 'E-INPUT-00016-MAX',
            'hirement.required' => 'E-INPUT-00017-REQ',
            'hirement.exists' => 'E-INPUT-00017-INV',
            'work_type.required' => 'E-INPUT-00018-REQ',
            'work_type.exists' => 'E-INPUT-00018-INV',
            'work_responsibility.required' => 'E-INPUT-00019-REQ',
            'work_responsibility.exists' => 'E-INPUT-00019-INV',
            'work_department.required' => 'E-INPUT-00020-REQ',
            'work_department.max' => 'E-INPUT-00020-MAX',
            'work_place_type.required' => 'E-INPUT-00021-REQ',
            'work_place_type.exists' => 'E-INPUT-00021-INV',

            'employment_capital.required' => 'E-INPUT-00023-REQ',
            'employment_capital.exists' => 'E-INPUT-00023-INV',
            'employee_quantity.required' => 'E-INPUT-00024-REQ',
            'employee_quantity.exists' => 'E-INPUT-00024-INV',
            'employment_start_date.required' => 'E-INPUT-00025-REQ',
            'employment_start_date.date' => 'E-INPUT-00025-DAT',
            'yearly_income.required' => 'E-INPUT-00026-REQ',
            'yearly_income.numeric' => 'E-INPUT-00026-NUM',
            'yearly_income.min' => 'E-INPUT-00026-MIN',
            'yearly_income.max' => 'E-INPUT-00026-MAX',
            'salary_type.required' => 'E-INPUT-00027-REQ',
            'salary_type.exists' => 'E-INPUT-00027-INV',
            'insurance.required' => 'E-INPUT-00028-REQ',
            'insurance.exists' => 'E-INPUT-00028-INV',
            'residence_place.required' => 'E-INPUT-00029-REQ',
            'residence_place.exists' => 'E-INPUT-00029-INV',
            'rent.required' => 'E-INPUT-00030-REQ',
            'rent.numeric' => 'E-INPUT-00030-NUM',
            'rent.min' => 'E-INPUT-00030-MIN',
            'rent.max' => 'E-INPUT-00030-MAX',
            'balance.required' => 'E-INPUT-00031-REQ',
            'balance.numeric' => 'E-INPUT-00031-NUM',
            'balance.min' => 'E-INPUT-00031-MIN',
            'balance.max' => 'E-INPUT-00031-MAX',
            'has_borrowing.required' => 'E-INPUT-00032-REQ',
            'has_borrowing.numeric' => 'E-INPUT-00032-NUM',
            'has_borrowing.exists' => 'E-INPUT-00032-INV',
            'auto_loan.required' => 'E-INPUT-00033-REQ',
            'auto_loan.exists' => 'E-INPUT-00033-INV',
            'cash_loan.required' => 'E-INPUT-00034-REQ',
            'cash_loan.exists' => 'E-INPUT-00034-INV',
            'other_loan.required' => 'E-INPUT-00035-REQ',
            'other_loan.exists' => 'E-INPUT-00035-INV',
            'monthly_autoloan_repayment.required' => 'E-INPUT-00036-REQ',
            'monthly_autoloan_repayment.numeric' => 'E-INPUT-00036-NUM',
            'monthly_autoloan_repayment.min' => 'E-INPUT-00036-MIN',
            'monthly_autoloan_repayment.max' => 'E-INPUT-00036-MAX',
            'monthly_cashloan_repayment.required' => 'E-INPUT-00037-REQ',
            'monthly_cashloan_repayment.numeric' => 'E-INPUT-00037-NUM',
            'monthly_cashloan_repayment.min' => 'E-INPUT-00037-MIN',
            'monthly_cashloan_repayment.max' => 'E-INPUT-00037-MAX',
            'monthly_otherloan_repayment.required' => 'E-INPUT-00038-REQ',
            'monthly_otherloan_repayment.numeric' => 'E-INPUT-00038-NUM',
            'monthly_otherloan_repayment.min' => 'E-INPUT-00038-MIN',
            'monthly_otherloan_repayment.max' => 'E-INPUT-00038-MAX',
            'usage_term_accepted.required' => 'E-INPUT-00039-REQ',
            'usage_term_accepted.numeric' => 'E-INPUT-00039-NUM',
            'usage_term_accepted.max' => 'E-INPUT-00039-MAX',
            'usage_term_accepted.min' => 'E-INPUT-00039-MIN',
            'temporary_member_id.required' => 'E-INPUT-00040-REQ',
            'temporary_member_id.exists' => 'E-INPUT-00040-INV',
        ];
        return $msg;
    }
}
